package com.android.equipment.ui

import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import com.android.equipment.base.BaseActivity
import com.android.equipment.bean.VersionBean
import com.android.equipment.dao.operate.DaoUser
import com.android.equipment.databinding.ActivityLoginBinding
import com.android.equipment.net.DialogCallback
import com.android.equipment.net.OkUtil
import com.android.equipment.net.ResponseBean
import com.android.equipment.utils.ToastUtil
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.lzy.okgo.model.Response

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    override fun setLayoutContent(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }


    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        if (DaoUser.getInstance().checkIsFirstLogin()) {
            DaoUser.getInstance().initSaveUser()
        }

        checkUpdateVersion()
    }

    override fun initEvent() {
        super.initEvent()


        binding.tvLogin.setOnClickListener { v: View? ->
            if (StringUtils.isEmpty(binding.etLoginAccount.text.toString())) {
                ToastUtil.showToastSafe("请输入手机号或用户名")
                return@setOnClickListener
            }
            if (StringUtils.isEmpty(binding.etPass.text.toString())) {
                ToastUtil.showToastSafe("请输入密码")
                return@setOnClickListener
            }

            val isOk = DaoUser.getInstance().checkUserLogin(
                binding.etLoginAccount.text.toString(),
                binding.etPass.text.toString()
            )
            if (isOk) {
                mSwipeBackHelper.forwardAndFinish(MainActivity::class.java)
            } else {
                ToastUtil.showToastSafe("您输入的账号密码有误")
            }
        }
    }


    override fun setStatusBar() {
        ImmersionBar.with(this)
            .init()
    }

    private fun checkUpdateVersion(){
        val packageManager: PackageManager = packageManager
        val packageInfo: PackageInfo = packageManager.getPackageInfo(packageName, 0)
        val versionCode = packageInfo.versionCode.toLong()

        OkUtil.getRequest<ResponseBean<VersionBean>>(
            "https://api.hdiyun.com/api-user/config-app/getByKey?key=bLJg8hG9",
            object : DialogCallback<ResponseBean<VersionBean>>(mContext) {
                override fun onSuccess(response: Response<ResponseBean<VersionBean>>) {
                    val data = response.body().getData()
                    if (data.versionCode > versionCode) {
                        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(data.androidUrl))
                        val chooser = Intent.createChooser(intent, "Choose a browser")
                        startActivity(chooser)
                    } else {
                        ToastUtils.showShort("当前已经是最新版本")
                    }
                }
            })
    }
}
